package com.monkeyk.sos.web.controller.portal.mobile;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.monkeyk.sos.domain.dto.ApplicationDetailsDto;
import com.monkeyk.sos.domain.dto.UserJsonDto;
import com.monkeyk.sos.infrastructure.DateUtil;
import com.monkeyk.sos.service.ApplicationService;
import com.monkeyk.sos.service.OauthService;
import com.monkeyk.sos.service.UserService;
import com.monkeyk.sos.web.controller.BaseController;
/**
 * 
* @ClassName: MobileController 
* @Description: 移动应用控制层 
* @author cygaod 
* @date 2015年12月10日 下午4:55:12 
* @version 1.0
 */
@Controller
@RequestMapping("/portal/mobile")
public class MobileController extends BaseController {
private static final Logger Log = Logger.getLogger(MobileController.class);
    @Autowired
    private UserService userService;
    
    @Autowired
    private OauthService  oauthService;
    
    @Autowired
    private ApplicationService  applicationService;

    @RequestMapping("/dashboard")
    public String dashboard() {
        return "mobile/dashboard";
    }
    
   
    
    /*手机端应用申请*/
    @RequestMapping("/mobileDetail")
    public String mobileDetail(Model model) {
    	Log.info("mobileDetail ："+"moblie/mobileDetail");
    	Map<String,String> resultMap = oauthService.createClientIdAndSecret();
    	ApplicationDetailsDto appDetailsDto = new ApplicationDetailsDto();
    	appDetailsDto.setClientId(resultMap.get("clientId"));
    	appDetailsDto.setClientSecret(resultMap.get("clientSecret"));
    	appDetailsDto.setCreateTime(DateUtil.getTodays());
    	model.addAttribute("appDetailsDto", appDetailsDto);
    	model.addAttribute("status", "first");
    	return "mobile/mobile_Detail";
    }
    
    /*手机端应用详情*/
    @RequestMapping("/mobileDetail/{appId}")
    public String mobileDetail(@PathVariable("appId") String appId, Model model) {
    	Log.info("mobileDetail/appId ："+appId);
    	ApplicationDetailsDto appDetailsDto = applicationService.getApplicationDetailsDto(appId);
    	model.addAttribute("appDetailsDto", appDetailsDto);
    	return "mobile/mobile_Detail";
    }
    
    /*保存应用详情*/
    @RequestMapping(value="/registerApplicationDetails",method = RequestMethod.POST)
    public String submitRegisterApplication(ApplicationDetailsDto appDetails,HttpServletRequest request, HttpServletResponse response) {
    	String appId = appDetails.getAppId();
    	/*if( appId != null && appId !=""){
    		applicationService.updateApplicationDetails(appDetails,appId);
    	}else {
    		appId = applicationService.registerApplicationDetails(appDetails);
		}*/
    	return "redirect:mobileDetail/"+appId;
    }
    
    @RequestMapping("user_info")
    @ResponseBody
    public UserJsonDto userInfo() throws Exception {
        return userService.loadCurrentUserJsonDto();
    }
}